package hu.szyrt.bug.dialogs;

import hu.szyrt.bug.R;
import android.app.Dialog;
import android.content.Context;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class BUGDialog extends Dialog {

	LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
	View layout;
	TextView title, content;
	Button positive, negative;
		
	public BUGDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { super(context, cancelable, cancelListener); init(); }
	public BUGDialog(Context context, int theme) { super(context, theme); init(); }
	public BUGDialog(Context context) { super(context, R.style.DialogTheme); init(); }
	private void init(){
		requestWindowFeature(Window.FEATURE_NO_TITLE);		
		
		layout 		= View.inflate(getContext(), R.layout.dialog, null);
		title 		= (TextView)layout.findViewById(R.id.title);
		content 	= (TextView)layout.findViewById(R.id.content);		
		positive 	= (Button)layout.findViewById(R.id.positiveButton);
		negative    = (Button)layout.findViewById(R.id.negativeButton);
				
		setContentView(layout, params);
	}
	
	@Override
	public void setTitle(CharSequence title) {
		this.title.setText(title);		
	}
	
	@Override
	public void setTitle(int titleId) {
		this.title.setText(titleId);
	}	
	
	public void setMessage(CharSequence message) {
		this.content.setText(message);
		this.content.setMovementMethod(new ScrollingMovementMethod());
	}
		
	public void setMessage(String content) {
		this.content.setText(content);
		this.content.setMovementMethod(new ScrollingMovementMethod());
	}
			
	public void setMessage(int contentId) {
		this.content.setText(contentId);		
		this.content.setMovementMethod(new ScrollingMovementMethod());
	}
	
	public void setPositiveButton(int resId, View.OnClickListener onClickListener){
		setPositiveButton(layout.getContext().getString(resId), onClickListener);
	}
	public void setPositiveButton(String text, View.OnClickListener onClickListener){
		this.positive.setText(text);
		this.positive.setOnClickListener(onClickListener == null ? dismissClickListener : onClickListener);
		this.positive.setVisibility(View.VISIBLE);
	}
	public void hidePositiveButton(){
		this.positive.setVisibility(View.GONE);
	}
	
	public void setNegativeButton(int resId, View.OnClickListener onClickListener){
		setNegativeButton(layout.getContext().getString(resId), onClickListener);
	}
	public void setNegativeButton(String text, View.OnClickListener onClickListener){
		this.negative.setText(text);
		this.negative.setOnClickListener(onClickListener == null ? dismissClickListener : onClickListener);
		this.negative.setVisibility(View.VISIBLE);
	}
	public void hideNegativeButton(){
		this.negative.setVisibility(View.GONE);
	}
	
	private View.OnClickListener dismissClickListener = new View.OnClickListener() {
		public void onClick(View v) {
			dismiss();
		}
	};
	
}
